package com.bosssoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.entity.dto.FibUserDTO;
import com.bosssoft.entity.po.FibUserPO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.mapper.FibUserMapper;
import com.bosssoft.service.FibUserService;
/**
 * @author Li kexi
 * Created on 2021/8/16.14:32
 */
@Slf4j
@Service
public class FibUserServiceImpl extends ServiceImpl<FibUserMapper, FibUserPO> implements FibUserService{


    @Override
    public boolean register(FibUserDTO userDTO) {
        if (userDTO != null) {
            FibUserDTO user = getUserByUsername(userDTO.getUserUsername());
            if (user != null) {
                log.info("用户名已存在！");
                return false;
            }
            FibUserPO userPO = new FibUserPO();
            try {
                BeanUtils.copyProperties(userDTO, userPO);
                return this.baseMapper.insert(userPO) > 0;
            } catch (Exception e) {
                throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
            }
        }
        return false;
    }

    @Override
    public FibUserDTO login(String username, String password) {
        QueryWrapper<FibUserPO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_username", username);
        wrapper.eq("user_password", password);
        FibUserPO userPO = this.baseMapper.selectOne(wrapper);
        try {
            if (userPO == null) {
                return null;
            }

            if (userPO.getDeleted() == 1) {
                FibUserDTO userDTO = new FibUserDTO();
                BeanUtils.copyProperties(userPO, userDTO);
                return userDTO;
            }
            log.info("账号不处于激活状态");
        } catch (Exception e) {
            log.info("登录异常!");
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
        return null;
    }

    /**
     * 通过用户名获取用户
     * @param username
     * @return
     */
    @Override
    public FibUserDTO getUserByUsername(String username) {
        log.info("进入getUserByUsername方法");
        try {
            FibUserDTO userDTO = new FibUserDTO();
            FibUserPO userPO = this.baseMapper.getUserByUsername(username);
            if (userPO == null) {
                return null;
            }
            BeanUtils.copyProperties(userPO, userDTO);
            return userDTO;
        } catch (Exception e) {
            log.info("getUserByUsername方法异常");
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    /**
     * 通过用户名获取id主键
     * @param username
     * @return
     */
    @Override
    public Long getIdByUsername(String username) {
        return this.baseMapper.getIdByUsername(username);
    }

    @Override
    public boolean updateUserDetails(FibUserDTO userDTO) {
        log.info("进入到updateUserDetails方法");
        if (this.baseMapper.selectById(userDTO.getUserId()) == null) {
            log.info("该用户暂未注册");
        }
        try {
            FibUserPO userPO = new FibUserPO();
            BeanUtils.copyProperties(userDTO, userPO);
            return this.baseMapper.updateById(userPO) > 0;
        } catch (Exception e) {
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @Override
    public boolean banUserById(Long id, Integer status) {
        return this.baseMapper.banUserById(id, status) > 0;
    }

    @Override
    public boolean banUserByUsername(String username, Integer status) {
        return this.baseMapper.banUserByUsername(username, status) > 0;
    }

    @Override
    public boolean deleteUserById(Long id) {
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteUserByUsername(String username) {
        return this.baseMapper.deleteUserByUsername(username) > 0;
    }

    @Override
    public int updateBatch(List<FibUserPO> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<FibUserPO> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<FibUserPO> list) {
        return baseMapper.batchInsert(list);
    }
    @Override
    public int insertOrUpdate(FibUserPO record) {
        return baseMapper.insertOrUpdate(record);
    }
    @Override
    public int insertOrUpdateSelective(FibUserPO record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    @Override
    public List<FibUserDTO> getAllUser() {
        try {
            List<FibUserPO> allUser = this.baseMapper.getAllUser();
            List<FibUserDTO> userDTOList = new ArrayList<>(allUser.size());
            for (int i = 0; i < allUser.size(); ++i) {
                userDTOList.add(new FibUserDTO());
                BeanUtils.copyProperties(allUser.get(i), userDTOList.get(i));
            }
            return userDTOList;
        } catch (Exception e) {
            log.info("出问题了");
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }
}
